package com.sjgtw.cloud.web.rescueTeam.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.rescueTeam.domain.RescueTeam;
import com.sjgtw.cloud.web.rescueTeam.domain.vo.RescueTeamImportVo;
import com.sjgtw.cloud.web.rescueTeam.service.IRescueTeamService;
import com.sjgtw.cloud.web.waterDamage.domain.WaterDamage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 抢险队伍Controller
 * 
 * @author ruoyi
 * @date 2024-03-09
 */
@RestController
@Api(tags="1.3.3、值班管理/抢险队伍")
@RequestMapping("/rescueTeam")
public class RescueTeamController extends BaseController
{
    @Autowired
    private IRescueTeamService rescueTeamService;

    /**
     * 查询抢险队伍列表
     */
   // @RequiresPermissions("system:抢险队伍:list")
    @ApiOperation("查询抢险队伍列表")
    @GetMapping("/list")
    public TableDataInfo list(RescueTeam rescueTeam)
    {
        startPage();
        List<Map<String,Object>> list = rescueTeamService.selectRescueTeamList(rescueTeam);
        return getDataTable(list);
    }

    /**
     * 新增抢险队伍
     */
    //@RequiresPermissions("system:抢险队伍:add")
    @PostMapping("/add")
    @ApiOperation("新增抢险队伍")
    public AjaxResult add(@RequestBody RescueTeam rescueTeam)
    {
        return toAjax(rescueTeamService.insertRescueTeam(rescueTeam));
    }

    /**
     * 修改抢险队伍
     */
    // @RequiresPermissions("system:抢险队伍:edit")
    @PutMapping("/edit")
    @ApiOperation("修改抢险队伍")
    public AjaxResult edit(@RequestBody RescueTeam rescueTeam)
    {
        return toAjax(rescueTeamService.updateRescueTeam(rescueTeam));
    }

    /**
     * 删除抢险队伍
     */
    //@RequiresPermissions("system:抢险队伍:remove")
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除抢险队伍")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(rescueTeamService.deleteRescueTeamById(id));
    }



    /**
     * 获取抢险队伍详细信息
     */
    //@RequiresPermissions("system:抢险队伍:query")
    @GetMapping(value = "/getInfo/{id}")
    @ApiOperation("获取抢险队伍详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(rescueTeamService.selectRescueTeamById(id));
    }


    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, RescueTeam rescueTeam)
    {
        List<RescueTeamImportVo> list = rescueTeamService.selectExportRescueTeamList(rescueTeam);

        EasyExcelUtil<RescueTeamImportVo> util = new EasyExcelUtil<>(RescueTeamImportVo.class);
        util.exportExcel(response, list, "抢险队伍", "抢险队伍", "rescueTeam.xlsx");
    }

    /**
     * 下载模版
     * @param response
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<RescueTeamImportVo> util = new EasyExcelUtil<>(RescueTeamImportVo.class);
        util.exportExcel(response, new ArrayList<>(), "抢险队伍", "抢险队伍模版", "rescueTeam.xlsx");
    }


    /**
     * 导入
     * @param file
     * @return
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<RescueTeamImportVo> util = new EasyExcelUtil<>(RescueTeamImportVo.class);
        List<RescueTeamImportVo> entities = util.readExcel(file, 0, 2);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }

        List<String> errors = rescueTeamService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }

        rescueTeamService.batchSave(entities);
        return success();
    }


}
